package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatGameServerResponseParams;
import com.foolishbird.sdk.params.FloatKefuResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatKefu;
import com.foolishbird.sdk.params.bean.FloatMenu;
import com.foolishbird.sdk.params.bean.FloatUserInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.activity.FoolishBirdFloatExtensionActivity;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;
import com.squareup.picasso.Picasso;


public class FloatKefuFragment extends BaseHttpFragment implements View.OnClickListener {


//    private LinearLayout phoneLayout,realnameLayout,versionLayout,pwdLayout;
    private TextView titleTxt,kefuTitle,kefuSubTitle,onlineText;
    private ImageView backView;
    TextView copyTxt,kefuTxt;
    ImageView codeIamge;
    private LinearLayout onlineBtn;

    public static final FloatKefuFragment newInstance(Bundle bundle) {
        FloatKefuFragment fragment = new FloatKefuFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_float_kefu");
        View contentView =inflater.inflate(resource,null);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("联系客服");
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        codeIamge = contentView.findViewById(getViewIdentifier("float_kefu_icon"));
        kefuTxt = contentView.findViewById(getViewIdentifier("float_kefu_name"));
        kefuTitle = contentView.findViewById(getViewIdentifier("float_kefu_title"));
        kefuSubTitle = contentView.findViewById(getViewIdentifier("float_kefu_subtitle"));
        onlineText = contentView.findViewById(getViewIdentifier("float_kefu_click_text"));
        onlineBtn = contentView.findViewById(getViewIdentifier("float_kefu_click"));
        onlineBtn.setOnClickListener(this);
        copyTxt = contentView.findViewById(getViewIdentifier("float_binded_copy"));
        copyTxt.setOnClickListener(this);
        HttpEngineApi.getInstance().getKefuInfo(requestCallbackHandler(FloatKefuResponseParams.class));
        return contentView;

    }

    private FloatUserInfo userInfo;



    @Override
    public void onClick(View view) {
        if(view == backView){
//            if(userInfo!=null&&!TextUtils.isEmpty(userInfo.getId_number())){
//
//            }
            MyFragmentManger.getInstance().goBack(this);
        }else if(view == copyTxt){
            //获取剪贴板管理器：
            ClipboardManager cm = (ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);

            String temp = (String)kefuTxt.getTag();

            // 创建普通字符型ClipData
            ClipData mClipData = ClipData.newPlainText("Label", temp);
            // 将ClipData内容放到系统剪贴板里。
            cm.setPrimaryClip(mClipData);
            Toast.makeText(getContext(),"复制成功",Toast.LENGTH_SHORT).show();
        }else if(view == onlineBtn){
            if(kefu==null)return;
            FloatMenu menu =new FloatMenu();
            menu.setType(2);
            menu.setName("正在前往微信");
            menu.setLink(kefu.getOnlineUrl());
            MyFragmentManger.getInstance().goDefaultFragment(menu);
        }

//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }

    FloatKefu kefu;

    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof FloatKefuResponseParams){
                    kefu =((FloatKefuResponseParams)response).getData().getCustomer();
                    if(kefu!=null){
                        kefuTxt.setText("微信号："+kefu.getWx_acount());
                        kefuTxt.setTag(kefu.getWx_acount());
                        kefuTitle.setText(kefu.getTitle());
                        kefuSubTitle.setText(kefu.getSubTitle());
                        onlineText.setText(kefu.getOnlineText());
                        Picasso.with(getContext()).load(kefu.getWx_qrcode()).into(codeIamge);
                    }
                }
            }

        }



        WL.d("FloatNoticeFragment",response.toString());
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
